﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page {
	ServerProvider.ServiceSoapClient cl = new ServerProvider.ServiceSoapClient();

	protected void Page_Load(object sender, EventArgs e) {
		if (!IsPostBack) {
			Page.ClientScript.RegisterStartupScript(this.GetType(),
				"ScrollTextbox",
				"<script type=\"text/javascript\">document.getElementById('" +
				this.chat.ClientID +
				"').scrollTop = document.getElementById('" +
				this.chat.ClientID +
				"').scrollHeight; " +
				" </script>");
		}

		if (Session["loggedIn"] == null) {
			Session["loggedIn"] = false;
			Response.Redirect("~/Login.aspx");
		}
		else if ((bool)Session["loggedIn"] == false)
			Response.Redirect("~/Login.aspx");
	}
	protected void logout_Click(object sender, EventArgs e) {
		string name = Session["userName"] as string;
		Session["userName"] = null;
		Session["loggedIn"] = false;
		cl.LogOut(name);
		Response.Redirect("~/Login.aspx");
	}
	protected void send_Click(object sender, EventArgs e) {
		string body = message.Text;
		message.Text = "";
		cl.PostMessage(body, Session["userName"] as string);
		Response.Redirect("~/Default.aspx");
	}
	protected void UpdatePanel1_Load(object sender, EventArgs e) {
		ServerProvider.ArrayOfAnyType[] newMessages = cl.GetNewMessages((DateTime)Session["dateTime"]);
		chat.Text = "";
		for (int i = 0; i < newMessages.Length; ++i)
			chat.Text += "(" + newMessages[i][2].ToString() + ") " + newMessages[i][0].ToString() + ":\n"
					+ newMessages[i][1].ToString() + "\n";
	}
	protected void UpdatePanel2_Load(object sender, EventArgs e) {
		usersList.Text = "";
		foreach (string item in cl.GetLoggedUsers())
			usersList.Text += item + "\n";
	}
}
